package ciyuan.Block.Machine.fkqgj;

import ciyuan.Gui.ContainerRegistry;
import ciyuan.Utils.TileEntityRegisterManager;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIntArray;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

/**
 * @author 150149
 */
public class FkqgjTileEntity extends TileEntity implements ITickableTileEntity, INamedContainerProvider {

    private final Inventory inventory = new Inventory(2);
    public ContainerProgress containerProgress = new ContainerProgress();

    public FkqgjTileEntity() {
        super(TileEntityRegisterManager.fkqgj.get());
    }

    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("ciyuan.container.dsc13");
    }

    @Nullable
    @Override
    public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new FkqgjContainer(ContainerRegistry.fkqgjContainer.get(), sycID, inventory, getBlockPos());
    }

    @Override
    public void load(BlockState state, CompoundNBT nbt) {
        this.inventory.setItem(0,ItemStack.of(nbt.getCompound("item")));
        this.inventory.setItem(1,ItemStack.of(nbt.getCompound("item2")));
        super.load(state, nbt);
    }

    @Override
    public CompoundNBT save(CompoundNBT compound) {
        ItemStack itemStack = this.inventory.getItem(0).copy();
        ItemStack itemStack2 = this.inventory.getItem(1).copy();
        compound.put("item", itemStack.serializeNBT());
        compound.put("item2", itemStack2.serializeNBT());
        return super.save(compound);
    }

    public Inventory getInventory() {
        return inventory;
    }

    @Override
    public void tick() {
        if (! this.hasLevel()) {
            return;
        }

        if (!this.inventory.getItem(0).isEmpty()) {
            String itemName = this.inventory.getItem(0).getItem().getDescriptionId();
            Item item = null;

            if (this.containerProgress.get(0)>=400 && this.inventory.canAddItem(new ItemStack(item))) {
                this.containerProgress.set(0,0);
                this.inventory.addItem(new ItemStack(item));
                this.inventory.getItem(0).setCount(this.inventory.getItem(0).getCount()-1);
            } else if (this.containerProgress.get(0)<400 && item!=null ) {
                this.containerProgress.set(0,this.containerProgress.get(0)+1);
            }

        } else {
            this.containerProgress.set(0,0);
        }
    }

    public class ContainerProgress implements IIntArray {

        int progress = 0;

        @Override
        public int get(int index) {
            return progress;
        }

        @Override
        public void set(int index, int value) {
            progress = value;
        }

        @Override
        public int getCount() {
            return 1;
        }
    }
}
